package com.template.entity.markdown;

import com.alibaba.fastjson2.JSONObject;

import static com.template.utlis.StringCommons.*;
import static com.template.utlis.StringCommons.CONTENT;
import static com.template.utlis.StringCommons.TagEnum.MARKDOWN;

/**
 * @author wangqi
 * @date 2024-01-18 11:28
 */
public class MarkdownElement {
    private StringBuilder stringBuilder;

    private JSONObject element;

    public MarkdownElement() {
        this.stringBuilder = new StringBuilder();
        this.element = new JSONObject();
        this.element.put(TAG, MARKDOWN.getValue());
    }

    public static MarkdownElement create() {
        return new MarkdownElement();
    }

    public static MarkdownElement createDefault(){
       return MarkdownElement.create().textAlign(TextAlignEnum.CENTER);
    }

    /**
     * 回车,下一行
     *
     * @return
     */
    public MarkdownElement nextLine() {
        stringBuilder.append("\n");
        return this;
    }

    /**
     * @return
     * @全体成员
     */
    public MarkdownElement atAll() {
        stringBuilder.append("<at id=all></at>");
        return this;
    }

    public MarkdownElement href(String url) {

        stringBuilder
                .append("<a href='")
                .append(translateContent(url))
                .append("'></a>");
        return this;
    }

    /**
     * 文字链接
     *
     * @param text 文字
     * @param url  链接
     * @return
     */
    public MarkdownElement textUrl(String text, String url) {
        stringBuilder.append("[").append(translateContent(text)).append("]")
                .append("(").append(translateContent(url)).append(")");
        return this;
    }

    /**
     * 分割线
     *
     * @return
     */
    public MarkdownElement splitLine() {
        stringBuilder.append("\n ---\n");
        return this;
    }

    /**
     * @param content       内容
     * @param bold          加粗
     * @param italic        斜体
     * @param strikethrough 删除线(中位线)
     * @param fontColorEnum 字体颜色
     * @return
     */
    public MarkdownElement content(String content, boolean bold, boolean italic, boolean strikethrough, FontColorEnum fontColorEnum) {
        if (fontColorEnum != null) {
            stringBuilder.append(fontColorEnum.getPrefix());
        }
        if (strikethrough) {
            stringBuilder.append("~~");
        }
        if (bold) {
            stringBuilder.append("**");
        }
        if (italic) {
            stringBuilder.append("*");
        }
        stringBuilder.append(translateContent(content));
        if (italic) {
            stringBuilder.append("*");
        }
        if (bold) {
            stringBuilder.append("**");
        }
        if (strikethrough) {
            stringBuilder.append("~~");
        }
        if (fontColorEnum != null) {
            stringBuilder.append(fontColorEnum.getSuffix());
        }
        return this;
    }

    /**
     * 加粗内容
     *
     * @param content
     * @return
     */
    public MarkdownElement boldContent(String content, FontColorEnum fontColorEnum) {
        return content(content, true, false, false, fontColorEnum);
    }

    /**
     * 加粗内容
     *
     * @param content
     * @return
     */
    public MarkdownElement boldContent(String content) {
        return content(content, true, false, false, null);
    }

    /**
     * 加粗中位线
     *
     * @param content
     * @return
     */
    public MarkdownElement boldAndStrikethroughContent(String content) {
        return content(content, true, false, true, null);
    }

    /**
     * 加粗中位线
     *
     * @param content
     * @return
     */
    public MarkdownElement boldAndStrikethroughContent(String content, FontColorEnum fontColorEnum) {
        return content(content, true, false, true, fontColorEnum);
    }

    /**
     * 斜体加粗内容
     */
    public MarkdownElement boldAndItalicContent(String content, FontColorEnum fontColorEnum) {
        return content(content, true, true, false, fontColorEnum);
    }

    /**
     * 斜体加粗内容
     */
    public MarkdownElement boldAndItalicContent(String content) {
        return content(content, true, true, false, null);
    }

    /**
     * 斜体内容
     *
     * @param content
     * @return
     */
    public MarkdownElement italicContent(String content, FontColorEnum fontColorEnum) {
        return content(content, false, true, false, fontColorEnum);
    }

    /**
     * 斜体内容
     *
     * @param content
     * @return
     */
    public MarkdownElement italicContent(String content) {
        return content(content, false, true, false, null);
    }

    /**
     * 斜体中位线
     * @param content
     * @return
     */
    public MarkdownElement italicAndStrikethroughContent(String content){
        return content(content, false, true, true, null);
    }

    /**
     * 斜体中位线
     * @param content
     * @return
     */
    public MarkdownElement italicAndStrikethroughContent(String content, FontColorEnum fontColorEnum){
        return content(content, false, true, true, fontColorEnum);
    }

    /**
     * 中位线内容(删除线内容)
     *
     * @param content
     * @param fontColorEnum
     * @return
     */
    public MarkdownElement strikethroughContent(String content, FontColorEnum fontColorEnum) {
        return content(content, false, false, true, fontColorEnum);
    }

    /**
     * 中位线内容(删除线内容)
     *
     * @param content
     * @return
     */
    public MarkdownElement strikethroughContent(String content) {
        return content(content, false, false, true, null);
    }

    public MarkdownElement content(String content, FontColorEnum fontColorEnum) {
        return content(content, false, false, false, fontColorEnum);
    }

    public MarkdownElement content(String content) {
        return content(content, false, false, false, null);
    }

    /**
     * 对齐方式
     *
     * @param textAlignEnum
     * @return
     */
    public MarkdownElement textAlign(TextAlignEnum textAlignEnum) {
        if (textAlignEnum != null) {
            element.put(TEXT_ALIGN, textAlignEnum.getValue());
        }
        return this;
    }

    public JSONObject build() {
        element.put(CONTENT, stringBuilder.toString());
        return element;
    }
}
